<template>
    <div>
        <div id="container"></div>
    </div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let map = null;

onMounted(() => {
    window._AMapSecurityConfig = {
        securityJsCode: "829ad09ae782570684d61f67a080c944",
    };
    AMapLoader.load({
        key: "b196cc6ae3600c969bd4ab4e5f434475", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Scale"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
    })
        .then((AMap) => {
            map = new AMap.Map("container", {
                resizeEnable: true,
                center: [116.397428, 39.90923],
                zoom: 13
            });
            map.clearMap();  // 清除地图覆盖物

            var markers = [{
                icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-1.png',
                position: [116.205467, 39.907761]
            }, {
                icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-2.png',
                position: [116.368904, 39.913423]
            }, {
                icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-3.png',
                position: [116.305467, 39.807761]
            }];

            // 添加一些分布不均的点到地图上,地图上添加三个点标记，作为参照
            markers.forEach(function (marker) {
                new AMap.Marker({
                    map: map,
                    icon: marker.icon,
                    position: [marker.position[0], marker.position[1]],
                    offset: new AMap.Pixel(-13, -30)
                });
            });

        })
        .catch((e) => {
            console.log(e);
        });
});

onUnmounted(() => {
    map?.destroy();
});
</script>

<style lang="scss" scoped>
#container {
    width: 800px;
    height: 800px;
}
</style>